package data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Observable;
/**
 * Chat message stores chat messages sent by the users.
 * @author Jie Zhao(288654)
 *
 */
public class ChatMessages extends Observable{
	private List<Message> messages = Collections.synchronizedList(new ArrayList<Message>());
	
	private static ChatMessages instance = null;
	
	
	private ChatMessages() {
	}
	
	/**
	 * Add a message to the message list.
	 * @param sender
	 * @param msg
	 */
	public synchronized void addMessage(String sender,String msg)
	{
		Message m = new Message(sender,msg);
		messages.add(m);
		setChanged();
		notifyObservers(m);
	}
	/**
	 * 
	 * @return an instance of chat messages
	 */
	public static ChatMessages getInstance()
	{
		if(instance == null)
		{
			instance = new ChatMessages();
		}
		return instance;
	}
	/**
	 * Chat message class, stores the sender and the text message
	 * @author Jie Zhao(288654)
	 *
	 */
	public class Message
	{
		public Message(String s,String m) {
			sender = s;
			msg = m;
			// TODO Auto-generated constructor stub
		}
		public String sender;
		public String msg;
	}
}
